﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Commands;
using PloobsEngine.Input;

namespace PloobsEngine.Behaviors
{
    public class KeyboardBehaviors : IBehaviors
    {
        private CommandProcessor CommandProcessor = CommandProcessor.getCommandProcessor();
        private IDictionary<string, IBehavior> _mapbehavior = new Dictionary<string, IBehavior>();
        private IDictionary<IInput, String> _mapbehaviorActDirect = new Dictionary<IInput, String>();
        private IDictionary<String, IInput> _mapbehavioractReverse = new Dictionary<String, IInput>();

        public void MapStringToBehavior(IBehavior beh, String act)
        {
            _mapbehavior[act] = beh;
        }

        public void MapKeyBehavior(Keys key, StateKey state,IBehavior beh , String act)
        {            
            InputPlayableKeyBoard ip1 = new SimpleConcreteKeyboardInputPlayable(state, key, Behave, EntityType.IOBJECT);
            _mapbehavior[act] = beh;
            _mapbehavioractReverse[act] = ip1;
            _mapbehaviorActDirect[ip1] = act;
            BindKeyCommand bkc1 = new BindKeyCommand(ip1, BindAction.ADD);
            this.CommandProcessor.SendCommandSyncronous(bkc1); 
            
        }

        public void RemoveMapKeyBehavior(String act)
        {
            _mapbehavior.Remove(act);
            _mapbehaviorActDirect.Remove(_mapbehavioractReverse[act]);
            BindKeyCommand bkc1 = new BindKeyCommand(_mapbehavioractReverse[act] as InputPlayableKeyBoard, BindAction.REMOVE);
            this.CommandProcessor.SendCommandSyncronous(bkc1); 
            _mapbehavioractReverse.Remove(act);
        }

        void Behave(InputPlayableKeyBoard ipk)
        {
            StartBehavior(_mapbehaviorActDirect[ipk], _mapbehaviorActDirect[ipk],null);
            //_mapbehavior[ipk].PerformBehavior(_mapbehaviorActDirect[ipk]);
        }

        public void StartBehavior(String act,String state,Object data)
        {
            _mapbehavior[act].PerformBehavior(state,data);
        }


        #region IBehaviors Members


        public T GetBehavior<T>(string Name)
        {
            return (T)_mapbehavior[Name];
        }
        
        #endregion

        #region IBehaviors Members


        public void ReleaseBehaviors()
        {
            foreach (string item in _mapbehavior.Keys)
            {
                _mapbehavior[item].Release();
            }
        }

        #endregion
    }
}
